package com.doradance.main;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.doradance.R;
import com.doradance.base.BaseFragment;
import com.doradance.main.adapter.MeAdapter;
import com.doradance.model.remote.IndexService;
import com.doradance.model.remote.bean.BannerListBean;
import com.doradance.model.remote.bean.HomeHotBean;
import com.doradance.model.remote.bean.HotListBean;
import com.doradance.net.FailureBean;
import com.doradance.net.HttpHandler;
import com.doradance.net.ServerField;
import com.doradance.net.ServerTaskObserver;
import com.doradance.view.listener.OnLoadMoreListener;
import com.yong.android.framework.utils.EasyToast;
import com.yong.android.framework.view.RefreshLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends BaseFragment {


    @Bind(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.mRefreshLayout)
    RefreshLayout mRefreshLayout;
    private MeAdapter mAdapter;

    @Override
    protected int getFragmentLayout() {
        return R.layout.fragment_me;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);

        mAdapter = new MeAdapter(getContext());
        setOnRefreshListener();
        setOnLoadMoreListener();
        setOnItemClickListener();
        mRefreshLayout.notifyRefresh();
        mRecyclerView.setAdapter(mAdapter);
        return rootView;
    }

    private void setOnItemClickListener() {
        mAdapter.setOnMeItemClickListener(new MeAdapter.OnMeItemClickListener() {
            @Override
            public void bannerClick(int position) {
            EasyToast.showToast(getContext(),"点击了"+position+"个");
            }

            @Override
            public void itemClick(int position) {
                EasyToast.showToast(getContext(),"点击了"+position+"个");
            }
        });
    }

    private void setOnLoadMoreListener() {
        mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                HttpHandler.create(IndexService.class)
                        .getHotList(mAdapter.getPage(),mAdapter.getPageSize())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new ServerTaskObserver<HotListBean>() {
                            @Override
                            public void onSuccess(HotListBean value) {
                                mAdapter.updateMore(value);
                            }

                            @Override
                            public void onFailure(FailureBean failureBean) {
                                mAdapter.notifyLoadMoreFailure();
                            }

                            @Override
                            public void onSubscribe(Disposable d) {

                            }
                        });

            }
        });
    }

    private void setOnRefreshListener() {
        mRefreshLayout.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                io.reactivex.Observable.combineLatest(HttpHandler.create(IndexService.class).getBannerList(), HttpHandler.create(IndexService.class).getHotList(ServerField.SERVER_FIRST_INDEX, mAdapter.getPageSize()), new BiFunction<BannerListBean, HotListBean, HomeHotBean>() {
                    @Override
                    public HomeHotBean apply(BannerListBean bannerListBean, HotListBean hotListBean) throws Exception {
                        HomeHotBean bean = new HomeHotBean();
                        bean.bannerListBean= bannerListBean;
                        bean.list=hotListBean;
                        return bean;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ServerTaskObserver<HomeHotBean>() {
                    @Override
                    public void onSuccess(HomeHotBean value) {
                        mAdapter.updateSource(value);
                        mRefreshLayout.notifiyRefreshComplete();
                    }

                    @Override
                    public void onFailure(FailureBean failureBean) {
                        mRefreshLayout.notifiyRefreshComplete();
                        mAdapter.notifyFailure();
                        EasyToast.showToast(getContext(),failureBean.failureMessage(getContext()));
                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }
                })
                ;
            }
        });


//
//        mRefreshLayout.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                HttpHandler.create(IndexService.class)
//                        .getHotList(ServerField.SERVER_FIRST_INDEX,mAdapter.getPageSize())
//                        .subscribeOn(Schedulers.io())
//                        .observeOn(AndroidSchedulers.mainThread())
//                        .subscribe(new ServerTaskObserver<HotListBean>() {
//                            @Override
//                            public void onSuccess(HotListBean value) {
//                                Log.e("AvatarAvatar",value.getList().get(0).getAvatar());
//                                mAdapter.updateSource(value);
//                                mRefreshLayout.notifiyRefreshComplete();
////                                mAdapter.updateSource(value == null ? null : value.getList());
//                            }
//
//                            @Override
//                            public void onFailure(FailureBean failureBean) {
//                                Log.e("AvatarAvatar",failureBean.failureMessage(getContext()));
//                            }
//
//                            @Override
//                            public void onSubscribe(Disposable d) {
//
//                            }
//                        });
//            }
//        });
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onResume() {
        super.onResume();

    }
}
